package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.pojo.DApplyApartment;
import org.example.pojo.DRepairman;
import org.example.pojo.DUser;
import org.example.service.DRepairmanService;
import org.example.mapper.DRepairmanMapper;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.*;

/**
* @author chenhongkai
* @description 针对表【d_repairman(维修工人)】的数据库操作Service实现
* @createDate 2024-05-27 09:39:56
*/
@Service
public class DRepairmanServiceImpl extends ServiceImpl<DRepairmanMapper, DRepairman>
    implements DRepairmanService{

    @Autowired
    private DRepairmanMapper dRepairmanMapper;

    @Override
    public Result<Map> SelectAllRepairmans(DRepairman dRepairman) {

        Page<DRepairman> page = new Page<>(dRepairman.getCurrentPage(),dRepairman.getPageSize());
        IPage<Map> mapIPage = dRepairmanMapper.SelectAllApplyApartment(page,dRepairman);

        Map repairmanMap = new HashMap<>();
        repairmanMap.put("pageSize",mapIPage.getSize());
        repairmanMap.put("pageTotal",mapIPage.getTotal());
        repairmanMap.put("repairmanData",mapIPage.getRecords());

        return Result.ok(repairmanMap);
    }

    @Override
    public Result<DRepairman> SelectOneRepairman(DRepairman dRepairman) {
        LambdaQueryWrapper<DRepairman> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DRepairman::getRmid,dRepairman.getRmid());
        DRepairman selectDRepairman = dRepairmanMapper.selectOne(lambdaQueryWrapper);
        return Result.ok(selectDRepairman);
    }

    @Override
    public Result<DRepairman> UpdateRepairmanById(DRepairman dRepairman) {

        LambdaQueryWrapper<DRepairman> RepairmanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        RepairmanLambdaQueryWrapper.eq(DRepairman::getRmid,dRepairman.getRmid());
        DRepairman selectRepairman1 = dRepairmanMapper.selectOne(RepairmanLambdaQueryWrapper);

        //是否改维修人员名称
        if(!Objects.equals(dRepairman.getRepairmanName(), selectRepairman1.getRepairmanName())){
            //是否重复维修人员名称
            LambdaQueryWrapper<DRepairman> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DRepairman::getRepairmanName,dRepairman.getRepairmanName());
            DRepairman selectRepairman2 = dRepairmanMapper.selectOne(lambdaQueryWrapper);
            if(selectRepairman2 != null)
                return Result.warning(501,"维修人员名称已经存在");
        }

        dRepairman.setUpdateDate(new Date());
        int row = dRepairmanMapper.updateById(dRepairman);
        if(row == 0) return Result.error("修改维修人员信息失败");
        return Result.ok(null);
    }

    @Override
    public Result<List<DRepairman>> SelectByType(DRepairman dRepairman) {
        List<DRepairman> selectRepairman = dRepairmanMapper.SelectOneByType(dRepairman);
        return Result.ok(selectRepairman);
    }

    @Override
    public Result<List<DRepairman>> AddRepairman(DRepairman dRepairman) {
        LambdaQueryWrapper<DRepairman> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DRepairman::getRepairmanName,dRepairman.getRepairmanName());
        DRepairman selectRepairman = dRepairmanMapper.selectOne(lambdaQueryWrapper);
        if(selectRepairman != null) {
            return Result.warning(501,"该维修人员名称已存在");
        }
        dRepairmanMapper.insert(dRepairman);
        return Result.ok(null);


    }

    @Override
    public Result<DRepairman> DeleteRepairman(DRepairman dRepairman) {
        int row = dRepairmanMapper.deleteById(dRepairman);
        if(row == 0 ) return Result.error("删除未休人员失败");
        return Result.ok(null);
    }

}




